﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "basic.h"  // 包含自定义结构体定义
#include "log.h"  //登录注册页面的函数声明
const char hotel_rooms[] = "D:\\Hotel\\Try1 - test1 - fix1_new\\room.txt"; //房间文件的地址
extern const char* EMPLOYEE_FILE; // 员工文件路径
extern Room* room_lists;
Room* roomdata = room_lists; // 全局变量，指向房间链表的头指针（4月8日新增）

extern Ord* cusdata; //全局变量，指向订单链表的头指针
extern float discount_rates[4]; // 折扣率数组: 普通,VIP,白金,团体
const char ord_address[100] = "D:\\Hotel\\Try1 - test1 - fix1_new\\order.txt"; //订单文件的地址
void front_desk_menu();
void check_in();
void check_out();
void show_rooms();
void show_orders();
void password_maintenance();
void sort_rooms_by_price();

// 声明函数
void Orders(Ord* cusdata);
void sort_By_SingleAttribute(Ord* cusdata, int attribute);
void sort_By_MultipleAttributes(Ord* cusdata, int attribute1, int attribute2);
void display_orders(Ord* cusdata);

void search_info();//新增信息查询

void modify_room_status_emp(int room_id, const char* new_status);

//统计功能函数声明
void Statistics_menu();


int judge_price_front(Ord* new_cus)
{
	//根据客户身份类型设置折扣
	if (strcmp(new_cus->customer_identity, "普通") == 0)
	{
		new_cus->room_price = new_cus->room_price * discount_rates[0];
		return 1;
	}
	else if (strcmp(new_cus->customer_identity, "VIP") == 0)
	{
		new_cus->room_price = new_cus->room_price * discount_rates[1];
		return 1;
	}
	else if (strcmp(new_cus->customer_identity, "白金") == 0)
	{
		new_cus->room_price = new_cus->room_price * discount_rates[2];
		return 1;
	}
	else if (strcmp(new_cus->customer_identity, "团体") == 0)
	{
		new_cus->room_price = new_cus->room_price * discount_rates[3];
		return 1;
	}
	else
	{
		printf("您填入的身份信息有误，请重新输入\n");
		return 0;
	}
}
//改变房间状态的函数
void change_room_status_cus(int room_id)
{
	//在房间链表中找到对应的房间
	Room* room = room_lists;
	while (room != NULL)
	{
		if (room->room_id == room_id)
		{
			//修改房间状态
			strcpy(room->room_state, "已入住");
			break;
		}
		room = room->next;
	}
	//将房间状态写入文件
	FILE* fp = fopen(hotel_rooms, "w");
	if (fp == NULL)
	{
		printf("无法打开文件\n");
		return;
	}
	Room* temp = room_lists;
	while (temp != NULL)
	{
		fprintf(fp, "%d %s %d %s\n", temp->room_id, temp->room_type, temp->room_price, temp->room_state);
		temp = temp->next;
	}
	fclose(fp);
	printf("房间状态已更新！\n");
}
// 1.新增订单
void add_order_front() {


	//1.添加客人到链表里面(这里用头插法),并且使用文件保存
	Ord* new_cus = (Ord*)malloc(sizeof(Ord));//申请一个新的客人
	//打开文件
	int lastOrderId = 1;
	//输入客人的基本信息
	//根据文件中最后一条记录的订单号来生成新的订单号
	if (cusdata == NULL)
	{
		lastOrderId = 1;
	}
	else
	{
		Ord* temp = cusdata;
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		lastOrderId = temp->order_id;
	}
	lastOrderId++;
	printf("请输入客人订购的房间号:");
	scanf("%d", &new_cus->room_id);
	printf("请输入客人订购的房间类型:");
	scanf("%s", new_cus->room_type);
	printf("请输入客人订购的房间价格:");
	scanf("%d", &new_cus->room_price);
	printf("请输入客人的订单状态:");
	scanf("%s", new_cus->order_state);
	printf("请输入客人的姓名:");
	scanf("%s", new_cus->customer_name);
	printf("请输入客人的电话:");
	scanf("%s", new_cus->customer_phone);
	printf("请输入客人的身份:(普通，VIP，白金，团体)");
	scanf("%s", new_cus->customer_identity);
	//判断用户输入是否出错
	while (judge_price_front(new_cus) == 0)
	{
		printf("请重新输入客人的身份:(普通，VIP，白金，团体)");
		scanf("%s", new_cus->customer_identity);
		return;
	}
	printf("||    请输入客人的入住时间:    ||");
	printf("||    注意，入住时间格式为：YYYYMMDDHHMM    ||\n");
	scanf("%s", new_cus->start_time);
	printf("||    请输入客人的离开时间:    ||");
	printf("||    注意，离开时间格式为：YYYYMMDDHHMM    ||\n");
	scanf("%s", new_cus->end_time);
	new_cus->order_id = lastOrderId;
	//修改房间状态
	change_room_status_cus(new_cus->room_id);
	// 将新的客人信息写入文件
	FILE* fp = fopen("D:\\Hotel\\Try1 - test1 - fix1_new\\order.txt", "a");
	if (fp == NULL)
	{
		printf("无法打开文件\n");
		return;
	}
	fprintf(fp, "%d %d %s %d %s %s %s %s %s %s\n",
		new_cus->order_id, new_cus->room_id, new_cus->room_type, new_cus->room_price,
		new_cus->order_state, new_cus->customer_name, new_cus->customer_phone, new_cus->customer_identity,
		new_cus->start_time, new_cus->end_time);
	printf("订单添加成功，订单号为: %d\n", new_cus->order_id);
	printf("请记住你的订单号，用于后续的操作\n");
	//关闭文件
	fclose(fp);
	new_cus->next = cusdata->next;//头插法
	cusdata->next = new_cus;
}


void front_desk_menu()
{
	int choice;
	do
	{
		printf("\n===========================================\n");
		printf("||               前台接待员功能            ||\n");
		printf("=============================================\n");
		printf("||      1. 办理入住                        ||\n");
		printf("||      2. 信息查询                        ||\n");
		printf("||      3. 信息统计                        ||\n");
		printf("||      4. 密码维护                        ||\n");
		printf("||      5. 查看房间信息                    ||\n");
		printf("||      6. 查看订单信息                    ||\n");
		printf("||      7. 信息排序                        ||\n");
		printf("||      8. 用户退房                        ||\n");
		printf("||      0. 返回上级菜单                    ||\n");
		printf("=============================================\n");
		printf("\n>>> 请选择操作 (0-7) : ");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			// 办理入住
			check_in();
			break;
		case 2:
			// 信息查询
			search_info();
			break;
		case 3:
			// 信息统计
			Statistics_menu();
			break;
		case 4:
			// 密码维护
			password_maintenance();
			break;
		case 5:
			// 查看房间信息
			show_rooms();
			break;
		case 6:
			// 查看订单信息
			show_orders();
			break;
		case 7:
			//信息排序
			Orders(cusdata);
			break;
		case 8:
			// 用户退房
			check_out();
			break;
		case 0:
			// 返回上级菜单
			return;
		default:
			printf("无效选择，请重新输入！\n");
		}
	} while (choice != 0);
}

//用户退房
void check_out()
{
	//办理退房
	printf("|||          欢迎来到退房界面             ||\n");
	printf("=============================================\n");
	printf("||		请输入顾客的订单号：              ||\n");
	int order_id;
	scanf("%d", &order_id);
	Ord* new_order = (Ord*)malloc(sizeof(Ord));
	//查找订单
	new_order = cusdata;
	while (new_order != NULL)
	{
		if (new_order->order_id == order_id)
		{
			break;
		}
		new_order = new_order->next;
	}
	if (new_order == NULL)
	{
		printf("未找到该订单！\n");
		return;
	}
	strcpy(new_order->order_state, "已退房");
	printf("退房成功！\n");
	modify_room_status_emp(new_order->room_id, "空闲");
	//将退房信息写入文件
	FILE* fp = fopen(ord_address, "w");
	if (fp == NULL)
	{
		printf("无法打开文件\n");
		return;
	}
	Ord* temp = cusdata;
	while (temp != NULL)
	{
		fprintf(fp, "%d %d %s %d %s %s %s %s %s %s\n",
			temp->order_id, temp->room_id, temp->room_type, temp->room_price,
			temp->order_state, temp->customer_name, temp->customer_phone,
			temp->customer_identity, temp->start_time, temp->end_time);
		temp = temp->next;
	}
	fclose(fp);
}


//查看订单信息
void show_orders()
{
	//显示所有订单信息
	printf("订单号\t房间号\t房间类型\t房间价格\t订单状态\t客户姓名\t客户电话\t客户身份\t入住时间\t离开时间\n");
	Ord* temp = cusdata;
	while (temp != NULL)
	{
		printf("%d\t%d\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n",
			temp->order_id, temp->room_id, temp->room_type, temp->room_price,
			temp->order_state, temp->customer_name, temp->customer_phone,
			temp->customer_identity, temp->start_time, temp->end_time);
		temp = temp->next;
	}
}
//修改房间状态
void modify_room_status_emp(int room_id, const char* new_status)
{
	Room* temp = roomdata;
	while (temp != NULL)
	{
		if (temp->room_id == room_id)
		{
			strcpy(temp->room_state, new_status);
			break;
		}
		temp = temp->next;
	}
	//将房间状态写入文件
	FILE* fp = fopen(hotel_rooms, "w");
	if (fp == NULL)
	{
		printf("无法打开文件\n");
		return;
	}
	Room* temp2 = roomdata;
	while (temp2 != NULL)
	{
		fprintf(fp, "%d %s %d %s\n",
			temp2->room_id, temp2->room_type, temp2->room_price, temp2->room_state);
		temp2 = temp2->next;
	}
	fclose(fp);
}
void check_in()
{
	//办理入住
	printf("您是否有预约？（1.有 2.没有）\n");
	int choice;
	scanf("%d", &choice);
	if (choice == 1)
	{
		//有预约
		printf("请输入您的订单号：\n");
		int order_id;
		scanf("%d", &order_id);
		Ord* new_order = (Ord*)malloc(sizeof(Ord));
		//查找订单
		new_order = cusdata;
		while (new_order != NULL)
		{
			if (new_order->order_id == order_id)
			{
				break;
			}
			new_order = new_order->next;
		}
		if (new_order == NULL)
		{
			printf("未找到该订单！\n");
			return;
		}
		if (new_order->room_id == ' ')
		{
			printf("该订单未预定房间！\n");
			printf("请输入房间号：\n");
			scanf("%d", &new_order->room_id);
			printf("请输入房间类型：\n");
			scanf("%s", new_order->room_type);
			printf("请输入房间价格：\n");
			scanf("%d", &new_order->room_price);
		}
		strcpy(new_order->order_state, "已入住");
		printf("入住成功！\n");
		//将房间状态改为已入住
		modify_room_status_emp(new_order->room_id, "已入住");
		//写入文件
		FILE* fp = fopen(ord_address, "w");
		Ord* temp = cusdata;
		if (fp == NULL)
		{
			printf("无法打开文件\n");
			return;
		}
		while (temp != NULL)
		{
			fprintf(fp, "%d %d %s %d %s %s %s %s %s %s\n",
				temp->order_id, temp->room_id, temp->room_type, temp->room_price,
				temp->order_state, temp->customer_name, temp->customer_phone,
				temp->customer_identity, temp->start_time, temp->end_time);
			temp = temp->next;
		}
	}
	else if (choice == 2)
	{
		//没有预约，现场入住
		add_order_front();
	}
	else
	{
		printf("无效选择！\n");
	}
}



// 密码维护功能 4.30更新
void password_maintenance()
{
	printf("请输入你要修改密码的账户名: ");
	char username[20];
	scanf("%s", username);

	char oldPassword[20];
	char newPassword[20];
	char confirmPassword[20];

	// 打开员工文件以读取原密码
	FILE* file = fopen(EMPLOYEE_FILE, "r");
	if (file == NULL)
	{
		printf("无法打开文件\n");
		return;
	}

	int found = 0;
	User admin;
	while (fscanf(file, "%s %s", admin.name, admin.password) != EOF)
	{
		if (strcmp(admin.name, username) == 0)
		{
			found = 1;
			break;
		}
	}
	fclose(file); // 关闭文件

	if (!found)
	{
		printf("未找到前台账户: %s\n", username);
		return;
	}

	printf("请输入当前密码: ");
	scanf("%s", oldPassword);

	// 验证输入的原密码是否正确
	if (strcmp(oldPassword, admin.password) != 0)
	{
		printf("当前密码错误！\n");
		return;
	}

	printf("请输入新的密码: ");
	scanf("%s", newPassword);
	printf("请再次确认新密码: ");
	scanf("%s", confirmPassword);

	// 检查两次输入的新密码是否一致
	if (strcmp(newPassword, confirmPassword) != 0)
	{
		printf("两次输入的新密码不一致！\n");
		return;
	}

	// 打开文件以更新密码
	file = fopen(EMPLOYEE_FILE, "r+");
	if (file == NULL)
	{
		printf("无法打开文件\n");
		return;
	}

	// 定位到当前管理员账号密码所在位置
	long position = 0;
	while (fscanf(file, "%s %s", admin.name, admin.password) != EOF)
	{
		if (strcmp(admin.name, username) == 0)
		{
			position = ftell(file) - strlen(admin.password) - 1; // 记录密码开始位置（减去换行符）
			break;
		}
	}

	if (position != 0) // 如果找到了对应账户
	{
		fseek(file, position, SEEK_SET); // 定位到密码开始位置
		fprintf(file, "%s\n", newPassword); // 写入新密码
		printf("密码修改成功！\n");
	}
	else
	{
		printf("未找到账户，密码修改失败！\n");
	}

	fclose(file); // 关闭文件
	return;
}

//4月7日新增函数

//新增查看房间信息
void show_rooms()
{
	Room* current = room_lists;
	printf("所有房间信息如下:\n");
	printf("房间号\t类型\t价格\t状态\n");
	while (current != NULL)
	{
		printf("%d\t%s\t%d\t%s\n", current->room_id, current->room_type, current->room_price, current->room_state);
		current = current->next;
	}
}
//新增信息查询功能
// 比较日期函数
int compare_dates(const char* date1, const char* date2)
{
	return strcmp(date1, date2);
}

// 信息查询函数
void search_info()
{
	while (1)
	{
		int choice;
		// 优化显示
		printf("=============================================\n");
		printf("||          信息查询功能                   ||\n");
		printf("=============================================\n");
		printf("|| 1. 查询某客房某时间段是否空             ||\n");
		printf("|| 2. 查询某客人预订信息                   ||\n");
		printf("|| 3. 组合查询（某客人某时间段的预订信息） ||\n");
		printf("|| 4. 模糊查询(客人姓名)                   ||\n");
		printf("|| 0. 返回上级菜单                         ||\n");
		printf("=============================================\n");
		scanf("%d", &choice);

		switch (choice)
		{
		case 1: {
			int room_id;
			char start_time[20], end_time[20];
			printf("请输入客房号：");
			scanf("%d", &room_id);
			printf("请输入开始时间（YYYY-MM-DD）：");
			scanf("%s", start_time);
			printf("请输入结束时间（YYYY-MM-DD）：");
			scanf("%s", end_time);

			Ord* temp = cusdata;
			int is_available = 1;
			while (temp != NULL)
			{
				if (temp->room_id == room_id)
				{
					if ((compare_dates(temp->start_time, end_time) < 0) && (compare_dates(temp->end_time, start_time) > 0))
					{
						is_available = 0;
						break;
					}
				}
				temp = temp->next;
			}
			if (is_available)
			{
				printf("该客房在该时间段为空。\n");
			}
			else
			{
				printf("该客房在该时间段已被预订。\n");
			}
			break;
		}
		case 2:
		{
			char customer_name[20];
			printf("请输入客人姓名：");
			scanf("%s", customer_name);

			Ord* temp = cusdata;
			int found = 0;
			while (temp != NULL)
			{
				if (strcmp(temp->customer_name, customer_name) == 0)
				{
					if (!found)
					{
						printf("订单号\t房间号\t房间类型\t房间价格\t订单状态\t客户姓名\t客户电话\t客户身份\t入住时间\t离开时间\n");
					}
					printf("%d\t%d\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n",
						temp->order_id, temp->room_id, temp->room_type, temp->room_price,
						temp->order_state, temp->customer_name, temp->customer_phone,
						temp->customer_identity, temp->start_time, temp->end_time);
					found = 1;
				}
				temp = temp->next;
			}
			if (!found)
			{
				printf("未找到该客人的预订信息。\n");
			}
			break;
		}
		case 3:
		{
			char customer_name[20];
			char start_time[20], end_time[20];
			printf("请输入客人姓名：");
			scanf("%s", customer_name);
			printf("请输入开始时间（YYYY-MM-DD）：");
			scanf("%s", start_time);
			printf("请输入结束时间（YYYY-MM-DD）：");
			scanf("%s", end_time);

			Ord* temp = cusdata;
			int found = 0;
			while (temp != NULL)
			{
				if (strcmp(temp->customer_name, customer_name) == 0)
				{
					if ((compare_dates(temp->start_time, end_time) < 0) && (compare_dates(temp->end_time, start_time) > 0))
					{
						if (!found)
						{
							printf("订单号\t房间号\t房间类型\t房间价格\t订单状态\t客户姓名\t客户电话\t客户身份\t入住时间\t离开时间\n");
						}
						printf("%d\t%d\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n",
							temp->order_id, temp->room_id, temp->room_type, temp->room_price,
							temp->order_state, temp->customer_name, temp->customer_phone,
							temp->customer_identity, temp->start_time, temp->end_time);
						found = 1;
					}
				}
				temp = temp->next;
			}
			if (!found)
			{
				printf("未找到该客人在该时间段的预订信息。\n");
			}
			break;
		}
		case 4:
		{
			// 模糊查询
			char customer_name[20];
			printf("请输入客人姓名（模糊查询）：");
			scanf("%s", customer_name);
			Ord* temp = cusdata;
			int found = 0;
			while (temp != NULL)
			{
				if (strstr(temp->customer_name, customer_name) != NULL)
				{
					if (!found)
					{
						printf("订单号\t房间号\t房间类型\t房间价格\t订单状态\t客户姓名\t客户电话\t客户身份\t入住时间\t离开时间\n");
					}
					printf("%d\t%d\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n",
						temp->order_id, temp->room_id, temp->room_type, temp->room_price,
						temp->order_state, temp->customer_name, temp->customer_phone,
						temp->customer_identity, temp->start_time, temp->end_time);
					found = 1;
				}
				temp = temp->next;
			}
			if (!found)
			{
				printf("未找到该客人的预订信息。\n");
			}
			break;
		}
		case 0:
			printf("返回上级菜单。\n");
			//返回前台接待员菜单
			front_desk_menu();
			break;
		default:
			printf("无效的选择，请重新输入！\n");
		}
	}
}


//新增
// 排序功能菜单
void Orders(Ord* cusdata)
{
	int choice;
	do {
		printf("\n=====================================\n");
		printf("||          排序功能菜单            ||\n");
		printf("=====================================\n");
		printf("|| 1. 按单一属性排序                ||\n");
		printf("|| 2. 按多属性排序                  ||\n");
		printf("|| 0. 返回上一级菜单                ||\n");
		printf("=====================================\n");
		printf("\n>>> 请选择操作 (0-2) : ");
		scanf("%d", &choice);
		switch (choice) {
		case 1: {
			int attribute;
			printf("请选择排序属性：\n");
			printf("1. 订单ID\n");
			printf("2. 房间号\n");
			printf("3. 房间价格\n");
			printf("4. 订单状态\n");
			printf("5. 客户姓名\n");
			scanf("%d", &attribute);
			sort_By_SingleAttribute(cusdata, attribute);
			printf("排序完成，以下是排序后的订单信息：\n");
			display_orders(cusdata);
			break;
		}
		case 2: {
			int attribute1, attribute2;
			printf("请选择第一个排序属性：\n");
			printf("1. 订单ID\n");
			printf("2. 房间号\n");
			printf("3. 房间价格\n");
			printf("4. 订单状态\n");
			printf("5. 客户姓名\n");
			scanf("%d", &attribute1);
			printf("请选择第二个排序属性：\n");
			printf("1. 订单ID\n");
			printf("2. 房间号\n");
			printf("3. 房间价格\n");
			printf("4. 订单状态\n");
			printf("5. 客户姓名\n");
			scanf("%d", &attribute2);
			sort_By_MultipleAttributes(cusdata, attribute1, attribute2);
			printf("排序完成，以下是排序后的订单信息：\n");
			display_orders(cusdata);
			break;
		}
		case 0:
			printf("返回上一级菜单。\n");
			break;
		default:
			printf("无效选择，请重新输入！\n");
		}
	} while (choice != 0);
}

void swapOrd(Ord* a, Ord* b) {
	if (a != b) {
		Ord temp = *a;
		*a = *b;
		*b = temp;

		// 交换next指针
		temp.next = a->next;
		a->next = b->next;
		b->next = temp.next;
	}
}
//按单一属性排序
void sort_By_SingleAttribute(Ord* cusdata, int attribute) {
	if (cusdata == NULL || cusdata->next == NULL) {
		return; // 链表为空或只有一个节点，无需排序
	}

	int swapped;
	Ord* ptr1;
	Ord* lptr = NULL; // 用于标记已排序部分的最后一个节点

	do {
		swapped = 0;
		ptr1 = cusdata;

		while (ptr1->next != lptr) { // 确保ptr1->next不为NULL
			int cmp = 0;

			// 根据选择的属性进行比较
			switch (attribute) {
			case 1: // 按订单ID排序
				cmp = ptr1->order_id > ptr1->next->order_id;
				break;
			case 2: // 按房间号排序
				cmp = ptr1->room_id > ptr1->next->room_id;
				break;
			case 3: // 按房间价格排序
				cmp = ptr1->room_price > ptr1->next->room_price;
				break;
			case 4: // 按订单状态排序
				cmp = strcmp(ptr1->order_state, ptr1->next->order_state) > 0;
				break;
			case 5: // 按客户姓名排序
				cmp = strcmp(ptr1->customer_name, ptr1->next->customer_name) > 0;
				break;
			}

			if (cmp) {
				swapOrd(ptr1, ptr1->next);
				swapped = 1;
			}
			ptr1 = ptr1->next;
		}
		lptr = ptr1;
	} while (swapped);
}

//按照多属性排序
void sort_By_MultipleAttributes(Ord* cusdata, int attribute1, int attribute2)
{
	if (cusdata == NULL || cusdata->next == NULL) {
		return; // 链表为空或只有一个节点，无需排序
	}
	int swapped;
	Ord* ptr1;
	Ord* lptr = NULL;
	do {
		swapped = 0;
		ptr1 = cusdata;
		while (ptr1->next != lptr) { // 确保ptr1->next不为NULL
			int cmp1 = 0, cmp2 = 0;
			// 根据选择的属性进行比较
			switch (attribute1) {
			case 1: // 按订单ID排序
				cmp1 = ptr1->order_id > ptr1->next->order_id;
				break;
			case 2: // 按房间号排序
				cmp1 = ptr1->room_id > ptr1->next->room_id;
				break;
			case 3: // 按房间价格排序
				cmp1 = ptr1->room_price > ptr1->next->room_price;
				break;
			}
			switch (attribute2) {
			case 1: // 按订单ID排序
				cmp2 = ptr1->order_id > ptr1->next->order_id;
				break;
			case 2: // 按房间号排序
				cmp2 = ptr1->room_id > ptr1->next->room_id;
				break;
			case 3: // 按房间价格排序
				cmp2 = ptr1->room_price > ptr1->next->room_price;
				break;
			}
			if (cmp1 || (!cmp1 && cmp2)) {
				swapOrd(ptr1, ptr1->next);
				swapped = 1;
			}
			ptr1 = ptr1->next;
		}
		lptr = ptr1;
	} while (swapped);
}
// 显示订单信息
void display_orders(Ord* cusdata)
{
	Ord* current = cusdata;
	printf("订单号\t房间号\t房间类型\t房间价格\t订单状态\t客户姓名\t客户电话\t入住时间\t离开时间\n");
	while (current != NULL) {
		printf("%d\t%d\t%s\t%d\t%s\t%s\t%s\t%s\t%s\n",
			current->order_id, current->room_id, current->room_type,
			current->room_price, current->order_state,
			current->customer_name, current->customer_phone,
			current->start_time, current->end_time);
		current = current->next;
	}
}




//4月9日新增信息统计功能
// 按入住时间和离开时间统计
void count_by_check_in_and_out_date()
{
	char check_in_date[10], check_out_date[10];
	printf("请输入入住时间: ");
	scanf("%s", check_in_date);
	printf("请输入离开时间: ");
	scanf("%s", check_out_date);
	int count = 0;
	for (Ord* order = cusdata->next; order != NULL; order = order->next)
	{
		if (strcmp(order->order_state, "已入住") == 0 && strcmp(order->start_time, check_in_date) == 0 && strcmp(order->end_time, check_out_date) == 0)
		{
			count++;
		}
	}
	printf("在 %s 入住并在 %s 离开的入住次数: %d\n", check_in_date, check_out_date, count);
}

// 统计某段时间内的入住率
void calculate_occupancy_rate_in_periods()
{
	char start_date[10], end_date[10];
	printf("请输入开始日期: ");
	scanf("%s", start_date);
	printf("请输入结束日期: ");
	scanf("%s", end_date);
	int total_rooms = 0;
	int occupied_rooms = 0;
	Room* current = room_lists;
	while (current != NULL)
	{
		total_rooms++;
		if (strcmp(current->room_state, "已入住") == 0)
		{
			for (Ord* order = cusdata->next; order != NULL; order = order->next)
			{
				if (order->room_id == current->room_id && strcmp(order->start_time, start_date) >= 0 && strcmp(order->start_time, end_date) <= 0)
				{
					occupied_rooms++;
					break;
				}
			}
		}
		current = current->next;
	}
	if (total_rooms == 0)
	{
		printf("没有房间信息！\n");
	}
	else
	{
		float occupancy_rate = (float)occupied_rooms / total_rooms * 100;
		printf("在 %s 到 %s 之间的入住率为: %.2f%%\n", start_date, end_date, occupancy_rate);
	}
}

// 统计某段时间内的预订率
void calculate_booking_rate_in_periods()
{
	char start_date[10], end_date[10];
	printf("请输入开始日期 : ");
	scanf("%s", start_date);
	printf("请输入结束日期: ");
	scanf("%s", end_date);
	int total_orders = 0;
	int booked_orders = 0;
	for (Ord* order = cusdata->next; order != NULL; order = order->next)
	{
		if (strcmp(order->start_time, start_date) >= 0 && strcmp(order->start_time, end_date) <= 0)
		{
			total_orders++;
			if (strcmp(order->order_state, "已预订") == 0)
			{
				booked_orders++;
			}
		}
	}
	if (total_orders == 0)
	{
		printf("没有订单信息！\n");
	}
	else
	{
		float booking_rate = (float)booked_orders / total_orders * 100;
		printf("在 %s 到 %s 之间的预订率为: %.2f%%\n", start_date, end_date, booking_rate);
	}
}

// 统计某客户在酒店的入住总天数
void calculate_total_stay_days_for_customers()
{
	char customer_name[20];
	printf("请输入客户姓名: ");
	scanf("%s", customer_name);
	int total_days = 0;
	for (Ord* order = cusdata->next; order != NULL; order = order->next)
	{
		if (strcmp(order->customer_name, customer_name) == 0) {
			long long start = atoi(order->start_time);
			long long end = atoi(order->end_time);
			total_days += end - start + 1;
		}
	}
	printf("客户 %s 在酒店的入住总天数: %d\n", customer_name, total_days);
}

// 统计某段时间内的入住率和预订率
void calculate_occupancy_and_booking_rate_in_periods()
{
	char start_date[10], end_date[10];
	printf("请输入开始日期： ");
	scanf("%s", start_date);
	printf("请输入结束日期: ");
	scanf("%s", end_date);
	int total_rooms = 0;
	int occupied_rooms = 0;
	int total_orders = 0;
	int booked_orders = 0;
	Room* current = room_lists;
	while (current != NULL)
	{
		total_rooms++;
		if (strcmp(current->room_state, "已入住") == 0)
		{
			for (Ord* order = cusdata->next; order != NULL; order = order->next)
			{
				if (order->room_id == current->room_id && strcmp(order->start_time, start_date) >= 0 && strcmp(order->start_time, end_date) <= 0) {
					occupied_rooms++;
					break;
				}
			}
		}
		current = current->next;
	}
	for (Ord* order = cusdata->next; order != NULL; order = order->next)
	{
		if (strcmp(order->start_time, start_date) >= 0 && strcmp(order->start_time, end_date) <= 0)
		{
			total_orders++;
			if (strcmp(order->order_state, "已预订") == 0)
			{
				booked_orders++;
			}
		}
	}
	if (total_rooms == 0)
	{
		printf("没有房间信息！\n");
	}
	else {
		float occupancy_rate = (float)occupied_rooms / total_rooms * 100;
		printf("在 %s 到 %s 之间的入住率为: %.2f%%\n", start_date, end_date, occupancy_rate);
	}
	if (total_orders == 0)
	{
		printf("没有订单信息！\n");
	}
	else
	{
		float booking_rate = (float)booked_orders / total_orders * 100;
		printf("在 %s 到 %s 之间的预订率为: %.2f%%\n", start_date, end_date, booking_rate);
	}
}

// 信息统计菜单
void Statistics_menu()
{
	int choice;
	do {
		printf("\n=====================================\n");
		printf("         前台信息统计页面            \n");
		printf("=====================================\n");
		printf(" 1. 按入住时间和离开时间统计     \n");
		printf(" 2. 统计某段时间内的入住率       \n");
		printf(" 3. 统计某段时间内的预订率       \n");
		printf(" 4. 统计某客户在酒店的入住总天数 \n");
		printf(" 5. 统计某段时间内的入住率和预订率 \n");
		printf(" 0. 返回上一级菜单               \n");
		printf("=====================================\n");
		printf("\n>>> 请选择操作 (0-5) : ");
		scanf("%d", &choice);

		switch (choice)
		{
		case 1:
			count_by_check_in_and_out_date();
			break;
		case 2:
			calculate_occupancy_rate_in_periods();
			break;
		case 3:
			calculate_booking_rate_in_periods();
			break;
		case 4:
			calculate_total_stay_days_for_customers();
			break;
		case 5:
			calculate_occupancy_and_booking_rate_in_periods();
			break;
		case 0:
			printf("返回上一级菜单。\n");
			break;
		default:
			printf("无效选择，请重新输入！\n");
		}
	} while (choice != 0);
}
